package com.flyone.fuxiao.controller;

import com.flyone.fuxiao.common.BaseResponse;
import com.flyone.fuxiao.common.ErrorCode;
import com.flyone.fuxiao.common.ResultUtils;
import com.flyone.fuxiao.exception.BusinessException;
import com.flyone.fuxiao.model.dto.postview.PostViewAddRequest;
import com.flyone.fuxiao.model.entity.User;
import com.flyone.fuxiao.service.PostViewService;
import com.flyone.fuxiao.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 帖子点赞接口
 */
@RestController
@RequestMapping("/post_view")
@Slf4j
public class PostViewController {

    @Resource
    private PostViewService postViewService;

    @Resource
    private UserService userService;
    /**
     * 增加查阅数
     *
     * @param postViewAddRequest
     * @param request
     * @return resultNum 本次点赞变化数
     */
    @PostMapping("/")
    public BaseResponse<Integer> doPostView(@RequestBody PostViewAddRequest postViewAddRequest,
                                         HttpServletRequest request) {
        if (postViewAddRequest == null || postViewAddRequest.getPostId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        int viewNum = postViewService.doPostView(postViewAddRequest.getPostId(), loginUser);
        // 登录才能点赞
        return ResultUtils.success(viewNum);
    }

}